/**
 * Copyright 2011 Snoopy Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.googlecode.snoopycp.ui;

import com.googlecode.snoopycp.core.Domain;
import javax.swing.DefaultListModel;
import javax.swing.JInternalFrame;


public class ChooseModuleInternalFrame extends javax.swing.JInternalFrame {

    private Domain domain;
    private DefaultListModel listAvaliableModel = new DefaultListModel();
    private DefaultListModel listDeploiedModel = new DefaultListModel();
    private NotepadInternalFrame notepad;

    /** Creates new form ChooseModuleInternalFrame */
    public ChooseModuleInternalFrame(Domain _domain, JInternalFrame _frame) {
        initComponents();
        domain = _domain;
        initList();
        this.setClosable(true);
        this.setTitle("Choose modules to deploy");
        notepad = (NotepadInternalFrame) _frame;
        this.btnOkChooseModule.setEnabled(false);
    }

    private void initList() {

        this.listAvaliable.setModel(listAvaliableModel);
        this.listDeploied.setModel(listDeploiedModel);

        for (Object host : domain.hosts().toArray()) {
            this.listAvaliableModel.addElement(host.toString());
        }

        this.listDeploiedModel.clear();
        //}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listAvaliable = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        listDeploied = new javax.swing.JList();
        btnMoveOne = new javax.swing.JButton();
        btnMoveAll = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnOkChooseModule = new javax.swing.JButton();
        btnRemain = new javax.swing.JButton();
        btnRemainAll = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        jScrollPane1.setViewportView(listAvaliable);

        jScrollPane2.setViewportView(listDeploied);

        btnMoveOne.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/googlecode/snoopycp/share/control.png"))); // NOI18N
        btnMoveOne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveOneActionPerformed(evt);
            }
        });

        btnMoveAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/googlecode/snoopycp/share/control-double.png"))); // NOI18N
        btnMoveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveAllActionPerformed(evt);
            }
        });

        jLabel1.setText("Avaliable");

        jLabel2.setText("Deploied");

        btnOkChooseModule.setText("Deploy");
        btnOkChooseModule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkChooseModuleActionPerformed(evt);
            }
        });

        btnRemain.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/googlecode/snoopycp/share/control-180.png"))); // NOI18N
        btnRemain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemainActionPerformed(evt);
            }
        });

        btnRemainAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/googlecode/snoopycp/share/control-double-180.png"))); // NOI18N
        btnRemainAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemainAllActionPerformed(evt);
            }
        });

        jCheckBox1.setText("Activate module after deploying");

        jLabel3.setText("Schedule");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Parameters");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnMoveAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnMoveOne, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
                            .addComponent(btnRemain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnRemainAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnOkChooseModule)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnMoveOne)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMoveAll)
                        .addGap(43, 43, 43)
                        .addComponent(btnRemain)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRemainAll))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOkChooseModule)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnMoveOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveOneActionPerformed
        if (!this.listAvaliable.isSelectionEmpty()) {
            int[] indexes = this.listAvaliable.getSelectedIndices();
            for (int index : indexes) {
                this.listDeploiedModel.addElement(this.listAvaliableModel.getElementAt(index).toString());
                this.listAvaliableModel.removeElementAt(index);
            }
            this.btnOkChooseModule.setEnabled(true);
        }
    }//GEN-LAST:event_btnMoveOneActionPerformed

    private void btnMoveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveAllActionPerformed
        if (!this.listAvaliableModel.isEmpty()) {
            this.listDeploiedModel.clear();
            for (Object host : domain.hosts().toArray()) {
                this.listDeploiedModel.addElement(host.toString());
            }
            this.listAvaliableModel.clear();
            this.btnOkChooseModule.setEnabled(true);
        }
    }//GEN-LAST:event_btnMoveAllActionPerformed

    private void btnRemainAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemainAllActionPerformed
        if (!this.listDeploiedModel.isEmpty()) {
            this.listAvaliableModel.clear();
            for (Object host : domain.hosts().toArray()) {
                this.listAvaliableModel.addElement(host.toString());
            }
            this.listDeploiedModel.clear();
            this.btnOkChooseModule.setEnabled(false);
        }
    }//GEN-LAST:event_btnRemainAllActionPerformed

    private void btnRemainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemainActionPerformed
        if (!this.listDeploied.isSelectionEmpty()) {
            int[] indexes = this.listDeploied.getSelectedIndices();
            for (int index : indexes) {
                this.listAvaliableModel.addElement(this.listDeploiedModel.getElementAt(index).toString());
                this.listDeploiedModel.removeElementAt(index);
            }
            if(this.listDeploiedModel.isEmpty()) {
                this.btnOkChooseModule.setEnabled(false);
            }
        }
    }//GEN-LAST:event_btnRemainActionPerformed

    private void btnOkChooseModuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkChooseModuleActionPerformed
        notepad.deploy(
                this.listDeploiedModel.toArray(), 
                this.jCheckBox1.isSelected(),
                this.jTextField1.getText(),
                this.jTextField2.getText());
        this.dispose();
    }//GEN-LAST:event_btnOkChooseModuleActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMoveAll;
    private javax.swing.JButton btnMoveOne;
    private javax.swing.JButton btnOkChooseModule;
    private javax.swing.JButton btnRemain;
    private javax.swing.JButton btnRemainAll;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JList listAvaliable;
    private javax.swing.JList listDeploied;
    // End of variables declaration//GEN-END:variables
}
